﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VkSample115;

public class ObjectData
{
    public float[] Data { get; set; }
    public int DataBytes { get; set; }
    public int VCount { get; set; }

    public void GenVertexData()
    {
        List<float> alVertex = new();
        List<float> alTexoor = new();
		float edgeLength = 24;//三角形边长
		int levelNum = 40;//三角形组层数
		float perLength = edgeLength / levelNum;//小三角形的边长
		for (int i = 0; i < levelNum; i++)//循环每一层生成小三角形
        {
			//当前层顶端边数
			int currTopEdgeNum = i;
			//当前层底端边数
			int currBottomEdgeNum = i + 1;
			//每个三角形的高度
			float currTrangleHeight = (float)(perLength * MathF.Sin(MathF.PI / 3));
			//当前层顶端最左边点的坐标
			float topEdgeFirstPointX = -perLength * currTopEdgeNum / 2;
			float topEdgeFirstPointY = -i * currTrangleHeight;
			float topEdgeFirstPointZ = 0;

			//当前层底端最左边点的坐标
			float bottomEdgeFirstPointX = -perLength * currBottomEdgeNum / 2;
			float bottomEdgeFirstPointY = -(i + 1) * currTrangleHeight;
			float bottomEdgeFirstPointZ = 0;
			//---------------纹理----------------
			float horSpan = 1 / (float)levelNum;//横向纹理的偏移量
			float verSpan = 1 / (float)levelNum;//纵向纹理的偏移量
												//当前层顶端最左边点的纹理ST坐标
			float topFirstS = 0.5f - currTopEdgeNum * horSpan / 2;
			float topFirstT = i * verSpan;
			////当前层底端最左边点的纹理ST坐标
			float bottomFirstS = 0.5f - currBottomEdgeNum * horSpan / 2;
			float bottomFirstT = (i + 1) * verSpan;

			//循环产生当前层各个上三角形的顶点数据
			for (int j = 0; j < currBottomEdgeNum; j++)
			{
				//当前三角形顶端点的X、Y、Z坐标
				float topX = topEdgeFirstPointX + j * perLength;
				float topY = topEdgeFirstPointY;
				float topZ = topEdgeFirstPointZ;
				//当前三角形顶端点的S、T纹理坐标
				float topS = topFirstS + j * horSpan;
				float topT = topFirstT;

				//当前三角形左下侧点的X、Y、Z坐标
				float leftBottomX = bottomEdgeFirstPointX + j * perLength;
				float leftBottomY = bottomEdgeFirstPointY;
				float leftBottomZ = bottomEdgeFirstPointZ;
				//当前三角形左下侧点的S、T纹理坐标
				float leftBottomS = bottomFirstS + j * horSpan;
				float leftBottomT = bottomFirstT;

				//当前三角形右下侧点的X、Y、Z坐标
				float rightBottomX = leftBottomX + perLength;
				float rightBottomY = bottomEdgeFirstPointY;
				float rightBottomZ = bottomEdgeFirstPointZ;
				//当前三角形右下侧点的S、T纹理坐标
				float rightBottomS = leftBottomS + horSpan;
				float rightBottomT = leftBottomT;
				//将当前三角形顶点数据按照逆时针顺序送入顶点坐标、纹理坐标列表
				//---顶点绘制方式
				alVertex.Add(topX); alVertex.Add(topY); alVertex.Add(topZ);
				alVertex.Add(leftBottomX); alVertex.Add(leftBottomY); alVertex.Add(leftBottomZ);
				alVertex.Add(rightBottomX); alVertex.Add(rightBottomY); alVertex.Add(rightBottomZ);
				//-------纹理绘制方式
				alTexoor.Add(topS); alTexoor.Add(topT);
				alTexoor.Add(leftBottomS); alTexoor.Add(leftBottomT);
				alTexoor.Add(rightBottomS); alTexoor.Add(rightBottomT);
			}
			for (int k = 0; k < currTopEdgeNum; k++)
			{//循环产生当前层各个下三角形的顶点数据
				float leftTopX = topEdgeFirstPointX + k * perLength;    //当前三角形左上侧
				float leftTopY = topEdgeFirstPointY;                //点的X、Y、Z坐标
				float leftTopZ = topEdgeFirstPointZ;
				float leftTopS = topFirstS + k * horSpan;               //当前三角形左上侧点的S、T纹理坐标
				float leftTopT = topFirstT;

				float bottomX = bottomEdgeFirstPointX + (k + 1) * perLength;//当前三角形底端点的X、Y、Z坐标
				float bottomY = bottomEdgeFirstPointY;
				float bottomZ = bottomEdgeFirstPointZ;
				float bottomS = bottomFirstS + (k + 1) * horSpan;       //当前三角形右底端点的S、T纹理坐标
				float bottomT = bottomFirstT;

				float rightTopX = leftTopX + perLength;             //当前三角形右上侧点的X、Y、Z坐标
				float rightTopY = leftTopY;
				float rightTopZ = leftTopZ;
				float rightTopS = leftTopS + horSpan;               //当前三角形右上侧点的S、T纹理坐标
				float rightTopT = topFirstT;
				//逆时针卷绕-----
				alVertex.Add(leftTopX); alVertex.Add(leftTopY); alVertex.Add(leftTopZ);
				alVertex.Add(bottomX); alVertex.Add(bottomY); alVertex.Add(bottomZ);
				alVertex.Add(rightTopX); alVertex.Add(rightTopY); alVertex.Add(rightTopZ);

				alTexoor.Add(leftTopS); alTexoor.Add(leftTopT);
				alTexoor.Add(bottomS); alTexoor.Add(bottomT);
				alTexoor.Add(rightTopS); alTexoor.Add(rightTopT);
			}
		}
		VCount = alVertex.Count() / 3;//顶点数量
		DataBytes = VCount * 5 * sizeof(float);//数据所占内存总字节数
		Data = new float[VCount * 5];
		int index = 0;//辅助索引
		for (int i = 0; i < VCount; i++)
		{
			Data[index++] = alVertex[i * 3 + 0];
			Data[index++] = alVertex[i * 3 + 1];
			Data[index++] = alVertex[i * 3 + 2];

			Data[index++] = alTexoor[i * 2 + 0];
			Data[index++] = alTexoor[i * 2 + 1];
		}
	}
}
